package com.galgram.project.controller;

import java.util.List;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;

import com.galgram.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.galgram.common.annotation.Log;
import com.galgram.common.core.controller.BaseController;
import com.galgram.common.core.domain.AjaxResult;
import com.galgram.common.enums.BusinessType;
import com.galgram.project.domain.FxCoupon;
import com.galgram.project.service.IFxCouponService;
import com.galgram.common.utils.poi.ExcelUtil;
import com.galgram.common.core.page.TableDataInfo;

/**
 * 优惠券Controller
 *
 * @author galgram
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/project/coupon")
public class FxCouponController extends BaseController {
    @Autowired
    private IFxCouponService fxCouponService;

    /**
     * 查询优惠券列表
     */
    @GetMapping("/getList")
    public TableDataInfo getCouponList(FxCoupon fxCoupon) {
        startPage();
        List<FxCoupon> list = fxCouponService.getCouponList(fxCoupon);
        return getDataTable(list);
    }

    /**
     * 使用优惠券
     */
    @PutMapping("/toUse")
    public AjaxResult useCoupon(@RequestBody FxCoupon fxCoupon) {
        fxCouponService.updateById(fxCoupon);
        return success("修改成功");
    }

    /**
     * 查询优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(FxCoupon fxCoupon) {
        startPage();
        List<FxCoupon> list = fxCouponService.selectFxCouponList(fxCoupon);
        return getDataTable(list);
    }

    /**
     * 获取优惠券详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fxCouponService.getById(id));
    }

    /**
     * 新增优惠券
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:add')")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FxCoupon fxCoupon) {
        fxCouponService.save(fxCoupon);
        return success("添加成功");
    }

    /**
     * 修改优惠券
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:edit')")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FxCoupon fxCoupon) {
        fxCouponService.updateById(fxCoupon);
        return success("修改成功");
    }

    /**
     * 删除优惠券
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:remove')")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        fxCouponService.removeByIds(ids);
        return success("删除成功");
    }


    /**
     * 导出优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:export')")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FxCoupon fxCoupon) {
        List<FxCoupon> list = fxCouponService.selectFxCouponList(fxCoupon);
        ExcelUtil<FxCoupon> util = new ExcelUtil<FxCoupon>(FxCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }

    /**
     * 导入优惠券列表
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:import')")
    @Log(title = "优惠券", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<FxCoupon> util = new ExcelUtil<FxCoupon>(FxCoupon.class);
        List<FxCoupon> userList = util.importExcel(file.getInputStream());
        fxCouponService.saveBatch(userList);
        return AjaxResult.success("导入成功");
    }

    /**
     * 下载模版
     */
    @PreAuthorize("@ss.hasPermi('project:coupon:export')")
    @PostMapping("/importTemplate")
    public void exportTemplate(HttpServletResponse response) {
        List<FxCoupon> list = new ArrayList<FxCoupon>();
        ExcelUtil<FxCoupon> util = new ExcelUtil<FxCoupon>(FxCoupon.class);
        util.exportExcel(response, list, "优惠券数据");
    }
}
